public class AtomService {

	private RestClient client;
	private String host;
	private String start;
	private String location;
	private HttpResponse response;
	private Integer counter;
	private final Integer MAX_REQUEST = 3;
	public Boolean isTest = false;
	public static String responseType = 'feed';
	public static String staticStart;

	public AtomService(String protocol, String host, String username, String password, Boolean isTest) {
		this(protocol, host, username, password);
		this.isTest = isTest;
	}
	
	public AtomService(String protocol, String host, String username, String password )
	{
		client 	= new RestClient();
		this.host 		= host;
		start 		= protocol + '://' + this.host;
		staticStart = start;
		this.location 	= null;
		this.response 	= null;
		this.counter 	= 0;
	
		try
		{  
			client.addCredentials(start, username, password);
		}
		
		catch(Exception ex)
		{
			System.debug(LoggingLevel.ERROR, 'AtamService - init\n\n' + ex.getMessage());
		}

	} 

	public String getStartUrl() {
		return start;
	}
	
	public void setResponseType(String rtype) {
		responseType = rtype;
	}
	
	public AtomBase makeServiceCall(String atomPath) {
		AtomParser dom; 
		if (!isTest) {
			response = client.get(start + atomPath);
			dom = new AtomParser(response.getBody());
		} else {
			dom = new AtomParser(AtomTestData.getActivityService());
			System.debug(LoggingLevel.INFO, '\n\nTest data for service is: \n' + AtomTestData.getActivityService() + '\n\n');
		}
		
		return dom.ownerDocument().getChild();
	}
    
    private String get(String path) {
    	System.debug(LoggingLevel.INFO, '\n\n get from ' + path);
    	 
    	String responseText;
		client.isTest = isTest;
		    	
		response = client.get(path);
		System.debug(LoggingLevel.FINE, 'Response: ' + response);
		if (!isTest) {
			responseText = response.getBody();
    	} else {
    		responseText = AtomTestData.getActivityFeed();
    	} 
		
    	if ( (response != null && response.getStatusCode() == 200) || responseText != null) {
   	 		System.debug(LoggingLevel.FINE, '\n\nResponse:\n\n' + responseText + '\n');
        } else if  (response != null && (response.getStatusCode() == 301 || response.getStatusCode() == 302 ) ) {
        	responseText = null;
			System.debug(LoggingLevel.ERROR, '\n\nLooks like we have a redirect issue in getFeed...\n\n' + response);
    	} 
       	return responseText;
    }

    public AtomBase getFeed(String atomPath, Boolean isFQ) {
    	
    	String responseText;
    	if (isFQ) { 
    		responseText = get(atomPath);
    	} else {
    		responseText = get(start + atomPath);
    	}
    	
    	AtomBase feed = null;  
		try {   
        	if(responseText != null) {
	            AtomParser dom = new AtomParser(responseText);
	            feed = (AtomBase) dom.ownerDocument().getChild();
            } 
        } catch(Exception e) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR - AtomService - getFeed\n\n' + e.getMessage());
        }
       	return feed;
    }

	public AtomBase post(String path, AtomBase entry) {
		try {
			HttpResponse response;
			String responseText;

			client.isTest = isTest;
			response = client.post(start + path, entry.ToAPP());
			if (!isTest) {
				System.debug(LoggingLevel.INFO, '\n\nPOST Status: ' + response);
				System.debug(LoggingLevel.INFO, '\n\nPOST response: ' + response.getBody());
				responseText = response.getBody();
			} else {
				if (responseType == 'feed') {
					responseText = AtomTestData.getActivityFeed();
				} else if (responseType == 'entry') {
					responseText = AtomTestData.getActivityEntry();
				} else if (responseType == 'todo') {
					responseText = AtomTestData.getTodoEntry();
				} 
			} 
			
			entry = null;
			AtomParser dom;
			try {
				if (isTest) {
					dom = new AtomParser(responseText, responseType);
					entry = (AtomBase) dom.ownerDocument().getChild();
				} else if ( (response != null && response.getStatusCode() == 201) ) {
					dom = new AtomParser(responseText, responseType);
	   	 			entry = (AtomBase) dom.ownerDocument().getChild();
		        } else if (response.getStatusCode() == 302)	{
					System.debug(LoggingLevel.ERROR, '\n\nLooks like we have a redirect issue in getEntry...\n\n' + response);
				}			
			} catch(Exception e) 	{
				System.debug(LoggingLevel.ERROR, '\n\nAtomService - getEntry\n\n' + e.getMessage());
			}
			return entry;
		} catch (Exception ex) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR ON POST: ' + ex.getMessage() + '\n\n\n\n');
			return null;
		}
	}
}